

package FinalProject;


public class Ordinary extends Employee {
    private final double hourlyRate;
    private double hoursWorked;
    private double overtimeRate;

    public Ordinary(String name, String id, String department, double hourlyRate) {
        super(name, id, department);
        this.hourlyRate = hourlyRate;
        this.hoursWorked = 0;
        this.overtimeRate = 1.5; // 默认加班倍率1.5
    }

    @Override
    public double calculateSalary() throws WorkHourException {
        validateWorkHours();

        // 基本工资（前160小时）
        double baseSalary = Math.min(hoursWorked, 160) * hourlyRate;

        // 加班工资（超过160小时部分）
        double overtimeSalary = 0;
        if (hoursWorked > 160) {
            double overtimeHours = hoursWorked - 160;
            overtimeSalary = overtimeHours * hourlyRate * overtimeRate;
        }

        return baseSalary + overtimeSalary;
    }

    @Override
    public String generatePaySlip() throws WorkHourException {
        double salary = calculateSalary();
        double baseHours = Math.min(hoursWorked, 160);
        double overtimeHours = Math.max(hoursWorked - 160, 0);

        StringBuilder paySlip = new StringBuilder();
        paySlip.append("=============== 工资条 ===============\n");
        paySlip.append(String.format("姓名: %-10s 工号: %s\n", name, id));
        paySlip.append(String.format("部门: %-10s 类型: %s\n", department, getEmployeeType()));
        paySlip.append("--------------------------------------\n");
        paySlip.append(String.format("时薪: %.2f 元/小时\n", hourlyRate));
        paySlip.append(String.format("工作时长: %.1f 小时\n", hoursWorked));
        paySlip.append(String.format("- 正常工时: %.1f 小时\n", baseHours));

        if (overtimeHours > 0) {
            paySlip.append(String.format("- 加班工时: %.1f 小时\n", overtimeHours));
            paySlip.append(String.format("- 加班倍率: %.1f\n", overtimeRate));
        }

        paySlip.append("--------------------------------------\n");
        paySlip.append(String.format("应发薪资: %.2f 元\n", salary));
        paySlip.append("======================================\n");

        return paySlip.toString();
    }

    @Override
    public String getEmployeeType() {
        return "普通员工";
    }

    private void validateWorkHours() throws WorkHourException {
        if (hoursWorked < 0) {
            throw new WorkHourException("工作时长不能为负数: " + hoursWorked + " 小时");
        }
        if (hoursWorked > 300) {
            throw new WorkHourException("工作时长超过最大限制: " + hoursWorked + " 小时");
        }
    }

    // Getter和Setter
    public void setHoursWorked(double hours) {
        this.hoursWorked = hours;
    }

    public void setOvertimeRate(double rate) {
        this.overtimeRate = rate;
    }

    public double getHourlyRate() {
        return hourlyRate;
    }

    public double getHoursWorked() {
        return hoursWorked;
    }

    public double getOvertimeRate() {
        return overtimeRate;
    }
}